package cn.com.gridinfo.sooc.core.course.pay.activity;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremy.arad.Arad;
import com.jeremy.arad.utils.Log;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.SoocApplication;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.course.classes.activity.CourseDetailActivity;
import cn.com.gridinfo.sooc.core.course.pay.common.CodeConst;
import cn.com.gridinfo.sooc.core.course.pay.dao.PayDao;
import cn.com.gridinfo.sooc.core.my.order.MyOrderActivity;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 支付结果
 * Created by yingchunli on 2016/8/9.
 */
public class PayResultActivity extends BaseSoocActivity {

    @InjectView(R.id.pay_result_iv)
    TextView payResultTv;//支付结果文字
    PayDao dao;
    @InjectView(R.id.look_order_btn)
    Button lookOrderBtn;//查看我的订单按钮
    @InjectView(R.id.zf_success_ll)
    LinearLayout zfSuccessLl;//支付成功要显示的按钮
    @InjectView(R.id.zf_fail_ll)
    LinearLayout zfFailLl;//支付失败要显示的按钮
    private String from;//从哪个页面来的

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.course_payresult);
        ButterKnife.inject(this);
        dao = new PayDao(this);
        from = getIntent().hasExtra("from") ? getIntent().getStringExtra("from") : "";
        //判断是否从购买课程页面来的
        if (from.equals("buyCourse")) {
            //是
            payResultTv.setText("恭喜你！\n你已经成功加入课程~");
            lookOrderBtn.setVisibility(View.GONE);
        } else {
            //从支付订单/ 微信回调页面来的
            dao.orderResult(SoocApplication.orderId);
        }
        zfSuccessLl.setVisibility(View.GONE);
        zfFailLl.setVisibility(View.GONE);
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.PAY_RESULT:
                        if (dao.getOrderStatus().equals("0")) {
                            //支付失败
                            showFailInfo();
                        } else {
                            zfSuccessLl.setVisibility(View.VISIBLE);
                            zfFailLl.setVisibility(View.GONE);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "数据请求异常");
        }
    }

    /**
     * 显示失败的信息
     */
    void showFailInfo() {
        Drawable drawable;
        drawable = getResources().getDrawable(R.mipmap.course_pay_fail);
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        payResultTv.setCompoundDrawables(null, drawable, null, null);
        payResultTv.setText("支付失败");
        zfSuccessLl.setVisibility(View.GONE);
        zfFailLl.setVisibility(View.VISIBLE);
    }

    /**
     * 进入课程按钮的点击
     */
    @OnClick(R.id.join_course_btn)
    void onJoinCourseClick() {
        Intent intent = new Intent();
        intent.setClass(this, CourseDetailActivity.class);
        intent.putExtra("kcid", getIntent().getStringExtra("zf_kcfm"));
        intent.putExtra("kcfm", getIntent().getStringExtra("zf_kcfm"));
        intent.putExtra("from", "payResult");
        if (from.equals("buyCourse")) {
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        }
        startActivity(intent);
        finish();
    }

    /**
     * 查看我的订单按钮的点击
     */
    @OnClick(R.id.look_order_btn)
    void onLookOrderClick() {
        Intent intent = new Intent();
        intent.setClass(this, MyOrderActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        finish();
    }

    /**
     * 重新支付按钮的点击
     */
    @OnClick(R.id.zf_again_btn)
    void onPayAgainClick() {
        SoocApplication.isPayResult = true;
        finish();
    }

    @Override
    public String setupToolBarTitle() {
        return "支付结果";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void goBack() {
        //关闭支付相关页面
//        SoocApplication.getContext().exit();
        //从我的订单页面来的，返回到我的订单，更新数据，否则，返回到课程详情页，并更新数据
        Intent intent = new Intent();
        if (SoocApplication.fromJoinPayResult == 2) {
            intent.setClass(this, MyOrderActivity.class);
        } else {
            intent.setClass(this, CourseDetailActivity.class);
            intent.putExtra("kcid", Arad.preferences.getString("zf_kcid"));
            intent.putExtra("kcfm", Arad.preferences.getString("zf_kcfm"));
            intent.putExtra("from","payResult");
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);//它可以关掉所要到的界面中间的activity
        startActivity(intent);
        finish();
    }

    @Override
    public void onBackPressed() {
        goBack();
    }

    @Override
    public void onRequestFaild(int requestCode, String errorNo, String errorMessage) {
        Log.d("payrsult onRequestFailed!");
        switch (requestCode) {
            case CodeConst.PAY_RESULT://支付结果
                //
                showFailInfo();
                break;
        }
    }
}
